<template>
  <div class="page-layout">
    <el-row :gutter="20" ref="form">
      <el-col :span="6">
        <div class="grid-content bg-purple">
          钱包余额：<span style="font-size:28px; color:#288c29">{{ balance }}</span>
        </div>
      </el-col>
      <el-col :span="12">
        <el-form :inline="true" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"
          class="demo-ruleForm">
          <el-form-item label="" prop="money">
            <el-input v-model="ruleForm.money" placeholder="请输入金额"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即充值</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-divider></el-divider>
    <el-form :model="form" ref="form" label-width="60px" class="form-inline" label-suffix="：">
      <el-row :gutter="2">
        <el-col :lg="6" :xl="4"><el-form-item label="类型">
            <el-select v-model="form.expenseType" placeholder="请选择">
              <el-option label="收入" :value="1"></el-option>
              <el-option label="支出" :value="2"></el-option>
            </el-select></el-form-item></el-col>
        <el-button style="margin-left:10px" type="primary" @click="onSubmit">查询</el-button>
        <span style="margin-left:10px">收入合计：￥{{ income }}元</span>
        <span style="margin-left:10px">支出合计：￥{{ expend }}元</span>
      </el-row>
    </el-form>
    <el-table border size="mini" ref="table" :data="tableData" :height="tableHeight" :style="{ width: '100%' }">
      <el-table-column v-if="form.expenseType == 1" prop="tradeNo" label="交易号"> </el-table-column>
      <el-table-column v-if="form.expenseType == 2" prop="orderNum" label="订单号"> </el-table-column>
      <el-table-column prop="createTime" label="日期"> </el-table-column>
      <el-table-column prop="money" label="金额"> </el-table-column>
      <el-table-column prop="boxNo" label="原因">
        <template slot-scope="scope">
          {{ getLabel(expenseType, scope.row.expenseType * 1) + '-' + getLabel(tradeType, scope.row.tradeType * 1) }}
        </template>
      </el-table-column>
      <el-table-column prop="reportNumber" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.payStatus === '1'">待支付</el-tag>
          <el-tag v-if="scope.row.payStatus === '2'" type="success">支付成功</el-tag>
          <el-tag v-if="scope.row.payStatus === '3'" type="warning">支付失败</el-tag>
        </template>
      </el-table-column>
      
      <el-table-column prop="remark" label="备注"> </el-table-column>
    </el-table>
    <el-pagination style="margin-top: 10px" @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="pageNum" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <el-dialog title="充值信息" :visible.sync="dialogVisible" width="450px">
      <div>
        <!-- <i class="el-icon-success"></i>  -->
        <img style="width:60px; margin-left:145px" :src="moeny">
        <p>实付款：￥<span style="color:#000;font-size:30px;font-weight: bold;">{{ ruleForm.money }}元</span></p>
        <p>订单信息：为{{ username }}充值</p>
        <p>收款方：深汇保</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="determine">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="提示" :visible.sync="isExamine" width="450px" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
      <div>
        <!-- <i class="el-icon-success"></i>  -->
        <p>您的账户还未审核通过，请耐心等待</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="() => $router.go(-1)">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="设置支付密码" :visible.sync="walletPassSatus" width="450px" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
      <div>
        <!-- <i class="el-icon-success"></i>  -->
        <p>您还未设置支付密码，请先设置支付密码</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="() => $router.go(-1)">取 消</el-button>
        <el-button type="primary" @click="() => $router.push({path: '/payPassword'})">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { tradeType, payStatus, expenseType } from '@/utils/options'
import { getLabel } from "@/utils/func";
import moeny from '@/assets/moeny.png'
export default {
  data() {
    return {
      moeny,
      username: localStorage.getItem("username"),
      dialogVisible: false,
      getLabel,
      tradeType,
      payStatus,
      expenseType,
      activeName: 'first',
      ruleForm: {
        money: '',
      },
      income: 0,
      expend: 0,
      form: {
        expenseType: 1
      },
      rules: {
        money: [
          { required: true, message: '请输入金额', trigger: 'blur' },
          { validator: (rule, value, cb) => this.validMoney(rule, value, cb), trigger: 'blur' }
        ]
      },
      pageSize: 10,
      total: 0,
      pageNum: 1,
      tableHeight: 0,
      isShow: false,
      tableData: [],
      balance: 0,
      canvasZfb: '',
      walletPassSatus: false,
      isExamine: false
    };
  },
  methods: {
    determine() {
      const money = this.ruleForm.money
      this.$router.push({
        path: '/payment',
        query: {
          money
        }
      })
      this.dialogVisible = false
      this.ruleForm.money = ''
    },
    validMoney(rule, value, cb) {
      const reg = /^\d+(\.\d{1,2})?$/
      if (!reg.test(value)) {
        cb('金额最多有两位小数，且最小值为0.01元')
      }
      if (value < 0 || value == 0) {
        cb('金额最多有两位小数，且最小值为0.01元')
      }
      cb()
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.dialogVisible = true
        }
      })
      
    },
    handleSizeChange(val) {
      this.pageNum = 1
      this.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.getTableData()
    },
    setTableHeight() {
      this.$nextTick(() => {
        const bodyHeight = window.innerHeight;
        const queryHeight = this.$refs.form && this.$refs.form.$el && this.$refs.form.$el.offsetHeight;
        this.tableHeight = bodyHeight - queryHeight - 235 + "px";
      });
    },
    getTableData() {
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        expenseType: this.form.expenseType
      };
      this.$get("/customer/walletexpense/list", params).then((res) => {
        if (res) {
          this.tableData = res.rows
          this.total = res.total
        } else {
          this.tableData = [];
          this.total = 0
        }
      });
    },
    getWalletInfo() {
      this.$post("/customer/wallet/info").then((res) => {
        if (res && res.code * 1 === 200 ) {
          this.walletPassSatus = !res.data.walletPassSatus
          this.balance = res.data.balance
        } else {
          this.isExamine = true
          this.balance = 0
        }
      });
    },
    onSubmit() {
      this.pageNum = 1
      this.getTableData()
    },
    getTotal() {
      this.$get("/customer/walletexpense/total")
        .then((res) => {
          if (res && res.code * 1 === 200) {
            this.income = res.data.income || 0
            this.expend = res.data.expend || 0
          }
        })
    },
  },
  mounted() {
    this.getTotal()
    this.setTableHeight();
    window.addEventListener("resize", this.setTableHeight);
    this.getTableData();
    this.getWalletInfo()
  },
};
</script>
<style scoped lang="less">
#canvas {
  width: 200px !important;
  height: 200px !important;
}

#canvasZfb {
  width: 200px !important;
  height: 200px !important;
}

.time {
  /deep/.el-form-item__label {
    line-height: 20px;
  }
}
</style>